package com.ityuwen.mengyagushi.account.listener;

import com.ityuwen.mengyagushi.account.service.UserAccountService;
import com.ityuwen.mengyagushi.common.constant.KafkaConstant;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.support.Acknowledgment;
import org.springframework.stereotype.Component;

@Component
public class AccountListener {

    @Autowired
    private UserAccountService userAccountService;

    @KafkaListener(topics = KafkaConstant.QUEUE_USER_REGISTER)
    public void listen(String userId, Acknowledgment ack){
        if (StringUtils.isBlank(userId)){
            ack.acknowledge();
            return;
        }
        this.userAccountService.saveUserAccount(Long.valueOf(userId));
        ack.acknowledge();
    }

    @KafkaListener(topics = KafkaConstant.QUEUE_ACCOUNT_MINUS)
    public void minus(String orderNo, Acknowledgment ack){
        if (StringUtils.isBlank(orderNo)){
            ack.acknowledge();
            return;
        }

        this.userAccountService.minus(orderNo);
        ack.acknowledge();
    }

    @KafkaListener(topics = KafkaConstant.QUEUE_ACCOUNT_UNLOCK)
    public void unlock(String orderNo, Acknowledgment ack){
        if (StringUtils.isBlank(orderNo)){
            ack.acknowledge();
            return;
        }

        this.userAccountService.unlock(orderNo);
        ack.acknowledge();
    }

    @KafkaListener(topics = KafkaConstant.QUEUE_RECHARGE_PAY_SUCCESS)
    public void paySuccess(String orderNo, Acknowledgment ack){
        if (StringUtils.isBlank(orderNo)){
            ack.acknowledge();
            return;
        }

        this.userAccountService.paySuccess(orderNo);
        ack.acknowledge();
    }
}
